package managedbean.Discount; 
 
import entity.CRM.Discount;
import java.io.IOException;
import java.io.Serializable;
import java.util.List; 
import javaclass.ProductType;
import javax.ejb.EJB; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.CRM.DiscountBeanLocal; 
 
@ManagedBean
@ViewScoped 
public class ViewDiscountManagerBean implements Serializable{ 
    @EJB 
    private DiscountBeanLocal discountBeanLocal; 
    private Discount selectedDiscount;
    private String statusMessage;
 
    public ViewDiscountManagerBean() {}

    public Discount getSelectedDiscount() {
        return selectedDiscount;
    }
    public void setSelectedDiscount(Discount selectedDiscount) {
        this.selectedDiscount = selectedDiscount;
    }

    public String getStatusMessage() {
        return statusMessage;
    }
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<Discount> getDiscounts(){ 
        return discountBeanLocal.getDiscounts();
    }
    
    public void editDiscount(ActionEvent event) throws IOException{
        selectedDiscount = this.getSelectedDiscount();
        if (selectedDiscount != null){
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedDiscount", selectedDiscount);            
            FacesContext.getCurrentInstance().getExternalContext().redirect("editDiscount.xhtml");                
        }
    }
    
    public void deleteDiscount(ActionEvent event) throws IOException
    {
        selectedDiscount = this.getSelectedDiscount();
        try
        {
            discountBeanLocal.deleteDiscount(selectedDiscount.getDiscountId());            
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        
    }
    
    public List<Discount> getProductPriceA(){
        List<Discount> a_list = discountBeanLocal.getDiscountsByType(ProductType.NUTS);
        return a_list;
    }
    
    public List<Discount> getProductPriceB(){
        List<Discount> b_list = discountBeanLocal.getDiscountsByType(ProductType.FRUITS);
        return b_list;
    }
    
    public List<Discount> getProductPriceC(){
        List<Discount> c_list = discountBeanLocal.getDiscountsByType(ProductType.CHOCOLATE_NUTS);
        return c_list;
    }
    
    public List<Discount> getProductPriceD(){
        List<Discount> d_list = discountBeanLocal.getDiscountsByType(ProductType.REDCURRANT_FRUITS);
        return d_list;
    }
} 
